precision mediump float;
varying float vValue;
varying float vMin;
varying float vMax;

varying vec4 vClickRgba;
varying float vClick;

float rescale(float v, float min, float max, float newMin, float newMax){
    if(max-min<0.00001){
        max=max+0.00001;
    }
    return newMin + (newMax-newMin)*v/(max-min);
}

vec3 getJetColor(){
    float fWidth=(vMax-vMin)/5.;
    float f1=vMin+fWidth;
    float f2=f1+fWidth;
    float f3=f2+fWidth;
    float f4=f3+fWidth;
    float f5=f4+fWidth;
    if(vValue<vMin){
        return vec3(0.,0.,0.5);
    }else{
    if(vValue>vMin&&vValue<f1){
        return vec3(0., 0., rescale(vValue, vMin, f1, 0.5, 1.));
    }else{
    if(vValue>f1&&vValue<f2){
        return vec3(0., rescale(vValue, f1, f2, 0., 1.), 1.);
    }else{
    if(vValue>f2&&vValue<f3){
        return vec3(rescale(vValue, f2, f3, 0., 1.), 1., rescale(vValue, f2, f3, 1., 0.));
    }else{
    if(vValue>f3&&vValue<f4){
        return vec3(1., rescale(vValue, f4, f5, 0., 1.), 0.);
    }else{
    return (vValue>f4&&vValue<f5)?vec3(rescale(vValue, f5, vMax, 1., 0.5), 0., 0.):vec3(0.5,0.,0.0);
    }}}}}
}

void main(void) {
    vec3 jetColor = getJetColor();
    gl_FragColor = vClick>0.
        ?vClickRgba
        :vec4(jetColor,1.);
}